// packageFT/travel-order-client/detail/detail.js
const app = getApp()
import { dateRange, timeDiff, formatTime } from '../../../utils/util.js'
import { travelToolEnum } from '../../../utils/dataEnum'
// 增值项支付状态
const surchargeStatusEnum = {
  0: '未支付',
  1: '已支付' 
}
Page({

  /**
   * 页面的初始数据
   */
  data: {
    order_id: '',
    orderType: '',
    detailData: {},
    payBtn: null,
    travelToolEnum,
    surchargeStatusEnum
  },

  /**
   * 获取订单数据
   */
  getOrderDetail: function() {
    if(!this.data.order_id) return
    const urlStr = app.getNetAddresss('plugin.guide.frontend.order.personal-need.detail')
    const data = { oid: this.data.order_id,  }
    const _this = this
    wx.showLoading({ title: '加载中...' })
    app._getNetWork({
      url: urlStr,
      data,
      success: function(res) {
        const resData = res.data
        if (resData.result == 1) {
          // 支付按钮数据
          const payBtn = resData.data.button_models.find(item => item.value == 'pay')
          // 出行时间
          resData.data.timeRange = dateRange(resData.data.start_at, resData.data.finish_at)
          // 支付倒计时
          if (resData.data.pay_valid_time) {
            const now = new Date()
            const nowStr = formatTime(now)
            const countDownTime = timeDiff(nowStr, resData.data.pay_valid_time, 'millisecond')
              // 倒计时时间戳
            resData.data.countDownTime = countDownTime
          }
          _this.setData({ detailData: resData.data, payBtn })
          // 设置页面标题
          wx.setNavigationBarTitle({ title: resData.data.status_name })
        } else {
          app.tips(resData.msg || '数据加载失败！')
        }
      },
      complete: function()  {
        wx.hideLoading()
      }
    })
  },

  /**
   * 跳转到支付页面
   */
  goPay: function() {
    app.tips('需验证订单是否超时，待完善...')
    const { detailData, payBtn } = this.data
    if(!payBtn) return
    wx.navigateTo({
      url: `/packageD/buy/orderPay/orderPay?status=1&order_id=${detailData.order_id}&api=${payBtn.api}`,
    })
  },

  /**
   * 跳转到支付增值费页面
   */
  toPaySurcharge: function() {
    const surcharge = encodeURIComponent(JSON.stringify(this.data.detailData.has_one_surcharge))
    wx.navigateTo({
      url: '/packageFT/travel-order-client/pay-added/pay-added?surcharge=' + surcharge,
    })
  },

  _sentRequest: function(action, params) {

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      order_id: options?.order_id || '',
      orderType: options?.orderType || ''
    })
    this.getOrderDetail()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})